Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use evaluator for isolatedModules enum restrictions #57966

Conversation

andrewbranch
Copy link
Member

@andrewbranch andrewbranch commented Mar 27, 2024

With #57914, it becomes possible to enable isolatedModules in our own codebase. I tried this, but there were a few inconvenient changes required to enum declarations due to #56736. You can see them here: https://github.com/andrewbranch/TypeScript/compare/bug/37774...andrewbranch:TypeScript:enable-isolatedModules?expand=1

The checks added in #56736 seem to be a bit too restrictive. Specifically, references to same-file constants and other enum members were not recognized. I refactored the evaluator to return extra information that lets us produce the same errors with higher fidelity to the capabilities of the evaluator. It seems that transpilers (tested esbuild and Babel) have built their behavior around what our own evaluator can do; they’re just obviously limited to analyzing a single file (when not bundling). So, this PR removes isolatedModules errors from a lot of code that transpiles perfectly fine.

(cc @frigus02)

@typescript-bot typescript-bot added Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Mar 27, 2024
@jakebailey
Copy link
Member

The worst PR race; we just merged #57955 which moved this code.

@andrewbranch
Copy link
Member Author

@typescript-bot test it

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 27, 2024

Starting jobs; this comment will be updated as builds start and complete.

Command Status Results
test top400 ✅ Started ✅ Results
user test this ✅ Started ✅ Results
run dt ✅ Started ✅ Results
perf test this faster ✅ Started 👀 Results

@typescript-bot
Copy link
Collaborator

Looks like you're introducing a change to the public API surface area. If this includes breaking changes, please document them on our wiki's API Breaking Changes page.

Also, please make sure @DanielRosenwasser and @RyanCavanaugh are aware of the changes, just as a heads up.

@jakebailey
Copy link
Member

Uh oh, the last merge just broke the PR a lot

@andrewbranch
Copy link
Member Author

Yeah I was not on the branch I thought I was on 🤦🏻‍♂️

@andrewbranch andrewbranch force-pushed the bug/isolated-modules-enum-restrictions branch from 2d4e834 to 2035fc6 Compare March 27, 2024 23:21
@typescript-bot
Copy link
Collaborator

Hey @andrewbranch, the results of running the DT tests are ready.

Everything looks the same!

You can check the log here.

@typescript-bot
Copy link
Collaborator

@andrewbranch Here are the results of running the user tests comparing main and refs/pull/57966/merge:

Everything looks good!

@typescript-bot
Copy link
Collaborator

@andrewbranch
The results of the perf run you requested are in!

Here they are:

tsc

Comparison Report - baseline..pr
Metric baseline pr Delta Best Worst p-value
Angular - node (v18.15.0, x64)
Memory used 295,692k (± 0.01%) 295,678k (± 0.00%) ~ 295,663k 295,686k p=0.936 n=6
Parse Time 2.66s (± 0.24%) 2.66s (± 0.31%) ~ 2.65s 2.67s p=0.432 n=6
Bind Time 0.83s (± 0.62%) 0.83s (± 0.00%) ~ 0.83s 0.83s p=0.174 n=6
Check Time 8.24s (± 0.26%) 8.25s (± 0.13%) ~ 8.23s 8.26s p=1.000 n=6
Emit Time 7.02s (± 0.19%) 7.05s (± 0.39%) ~ 7.00s 7.07s p=0.142 n=6
Total Time 18.76s (± 0.11%) 18.78s (± 0.11%) ~ 18.75s 18.81s p=0.091 n=6
Compiler-Unions - node (v18.15.0, x64)
Memory used 195,095k (± 0.68%) 193,283k (± 0.97%) ~ 192,035k 195,817k p=0.230 n=6
Parse Time 1.65s (± 0.99%) 1.66s (± 1.32%) ~ 1.64s 1.70s p=0.805 n=6
Bind Time 0.88s (± 1.18%) 0.88s (± 1.33%) ~ 0.86s 0.89s p=0.801 n=6
Check Time 11.27s (± 0.47%) 11.22s (± 0.37%) ~ 11.18s 11.28s p=0.107 n=6
Emit Time 3.16s (± 0.47%) 3.13s (± 0.72%) ~ 3.11s 3.17s p=0.089 n=6
Total Time 16.96s (± 0.19%) 16.89s (± 0.29%) -0.07s (- 0.40%) 16.83s 16.98s p=0.045 n=6
Monaco - node (v18.15.0, x64)
Memory used 347,421k (± 0.01%) 347,517k (± 0.00%) +96k (+ 0.03%) 347,492k 347,537k p=0.005 n=6
Parse Time 3.68s (± 1.37%) 3.67s (± 0.72%) ~ 3.64s 3.71s p=1.000 n=6
Bind Time 1.38s (± 2.35%) 1.38s (± 0.59%) ~ 1.37s 1.39s p=1.000 n=6
Check Time 10.23s (± 0.48%) 10.24s (± 0.46%) ~ 10.16s 10.29s p=0.630 n=6
Emit Time 6.03s (± 0.49%) 6.02s (± 0.60%) ~ 5.98s 6.06s p=0.627 n=6
Total Time 21.31s (± 0.27%) 21.30s (± 0.25%) ~ 21.20s 21.34s p=0.809 n=6
TFS - node (v18.15.0, x64)
Memory used 302,767k (± 0.01%) 302,797k (± 0.01%) +30k (+ 0.01%) 302,761k 302,812k p=0.037 n=6
Parse Time 2.42s (± 0.85%) 2.39s (± 0.89%) -0.04s (- 1.44%) 2.36s 2.42s p=0.034 n=6
Bind Time 1.23s (± 1.08%) 1.19s (± 0.34%) 🟩-0.04s (- 3.26%) 1.18s 1.19s p=0.003 n=6
Check Time 7.51s (± 0.40%) 7.51s (± 0.22%) ~ 7.48s 7.53s p=0.871 n=6
Emit Time 4.26s (± 0.76%) 4.26s (± 0.75%) ~ 4.22s 4.31s p=0.809 n=6
Total Time 15.42s (± 0.25%) 15.35s (± 0.26%) -0.07s (- 0.48%) 15.30s 15.41s p=0.020 n=6
material-ui - node (v18.15.0, x64)
Memory used 510,066k (± 0.01%) 510,143k (± 0.01%) +77k (+ 0.02%) 510,078k 510,231k p=0.045 n=6
Parse Time 3.18s (± 0.44%) 3.19s (± 0.32%) ~ 3.17s 3.20s p=0.410 n=6
Bind Time 1.18s (± 1.07%) 1.18s (± 0.44%) ~ 1.18s 1.19s p=0.448 n=6
Check Time 20.56s (± 0.38%) 20.56s (± 0.25%) ~ 20.46s 20.60s p=0.872 n=6
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) ~ 0.00s 0.00s p=1.000 n=6
Total Time 24.92s (± 0.34%) 24.94s (± 0.20%) ~ 24.84s 24.97s p=0.415 n=6
mui-docs - node (v18.15.0, x64)
Memory used 1,744,450k (± 0.00%) 1,744,496k (± 0.00%) +46k (+ 0.00%) 1,744,448k 1,744,544k p=0.045 n=6
Parse Time 9.61s (± 0.60%) 9.62s (± 0.73%) ~ 9.51s 9.70s p=0.748 n=6
Bind Time 3.47s (± 1.71%) 3.48s (± 0.60%) ~ 3.44s 3.50s p=0.872 n=6
Check Time 82.18s (± 0.54%) 81.78s (± 0.47%) ~ 81.43s 82.48s p=0.199 n=6
Emit Time 0.19s (± 0.00%) 0.19s (± 4.22%) ~ 0.19s 0.21s p=0.405 n=6
Total Time 95.45s (± 0.49%) 95.06s (± 0.43%) ~ 94.62s 95.76s p=0.199 n=6
self-build-src - node (v18.15.0, x64)
Memory used 2,396,011k (± 0.04%) 2,397,757k (± 0.03%) +1,746k (+ 0.07%) 2,396,700k 2,398,381k p=0.013 n=6
Parse Time 5.96s (± 0.72%) 6.04s (± 0.79%) +0.07s (+ 1.26%) 5.99s 6.10s p=0.005 n=6
Bind Time 2.25s (± 0.83%) 2.27s (± 0.87%) ~ 2.24s 2.30s p=0.568 n=6
Check Time 39.82s (± 0.42%) 39.74s (± 0.26%) ~ 39.61s 39.85s p=0.521 n=6
Emit Time 3.15s (± 1.41%) 3.17s (± 3.66%) ~ 3.07s 3.39s p=1.000 n=6
Total Time 51.22s (± 0.35%) 51.24s (± 0.44%) ~ 51.00s 51.64s p=0.936 n=6
self-compiler - node (v18.15.0, x64)
Memory used 415,832k (± 0.01%) 416,010k (± 0.01%) +178k (+ 0.04%) 415,940k 416,053k p=0.005 n=6
Parse Time 3.34s (± 1.01%) 3.33s (± 0.48%) ~ 3.31s 3.35s p=0.568 n=6
Bind Time 1.30s (± 0.94%) 1.29s (± 0.69%) ~ 1.28s 1.30s p=0.445 n=6
Check Time 18.21s (± 0.25%) 18.24s (± 0.32%) ~ 18.17s 18.33s p=0.463 n=6
Emit Time 1.36s (± 0.72%) 1.36s (± 0.89%) ~ 1.34s 1.37s p=0.498 n=6
Total Time 24.21s (± 0.21%) 24.21s (± 0.27%) ~ 24.11s 24.31s p=0.627 n=6
vscode - node (v18.15.0, x64)
Memory used 2,896,320k (± 0.00%) 2,896,637k (± 0.00%) +317k (+ 0.01%) 2,896,566k 2,896,778k p=0.005 n=6
Parse Time 15.95s (± 0.29%) 15.96s (± 0.28%) ~ 15.91s 16.04s p=0.687 n=6
Bind Time 5.06s (± 0.59%) 5.05s (± 0.69%) ~ 5.01s 5.10s p=0.808 n=6
Check Time 87.48s (± 0.43%) 87.59s (± 0.49%) ~ 87.19s 88.42s p=0.936 n=6
Emit Time 23.77s (± 0.54%) 23.69s (± 0.80%) ~ 23.43s 23.90s p=0.470 n=6
Total Time 132.26s (± 0.32%) 132.30s (± 0.30%) ~ 131.83s 133.03s p=0.810 n=6
webpack - node (v18.15.0, x64)
Memory used 408,950k (± 0.03%) 408,855k (± 0.02%) ~ 408,752k 409,039k p=0.173 n=6
Parse Time 3.91s (± 0.65%) 3.89s (± 0.73%) ~ 3.85s 3.93s p=0.197 n=6
Bind Time 1.69s (± 0.31%) 1.68s (± 0.72%) ~ 1.66s 1.69s p=0.142 n=6
Check Time 16.81s (± 0.10%) 16.81s (± 0.25%) ~ 16.74s 16.85s p=0.372 n=6
Emit Time 0.00s (± 0.00%) 0.00s (± 0.00%) ~ 0.00s 0.00s p=1.000 n=6
Total Time 22.40s (± 0.16%) 22.38s (± 0.13%) ~ 22.34s 22.43s p=0.293 n=6
xstate - node (v18.15.0, x64)
Memory used 513,385k (± 0.00%) 513,386k (± 0.01%) ~ 513,338k 513,424k p=0.936 n=6
Parse Time 3.28s (± 0.36%) 3.27s (± 0.66%) ~ 3.23s 3.29s p=0.864 n=6
Bind Time 1.57s (± 0.52%) 1.57s (± 0.52%) ~ 1.56s 1.58s p=1.000 n=6
Check Time 2.91s (± 0.29%) 2.91s (± 0.52%) ~ 2.90s 2.94s p=0.503 n=6
Emit Time 0.07s (± 7.03%) 0.07s (±11.12%) ~ 0.07s 0.09s p=0.752 n=6
Total Time 7.84s (± 0.24%) 7.83s (± 0.22%) ~ 7.81s 7.86s p=0.469 n=6
System info unknown
Hosts
  • node (v18.15.0, x64)
Scenarios
  • Angular - node (v18.15.0, x64)
  • Compiler-Unions - node (v18.15.0, x64)
  • Monaco - node (v18.15.0, x64)
  • TFS - node (v18.15.0, x64)
  • material-ui - node (v18.15.0, x64)
  • mui-docs - node (v18.15.0, x64)
  • self-build-src - node (v18.15.0, x64)
  • self-compiler - node (v18.15.0, x64)
  • vscode - node (v18.15.0, x64)
  • webpack - node (v18.15.0, x64)
  • xstate - node (v18.15.0, x64)
Benchmark Name Iterations
Current pr 6
Baseline baseline 6

Developer Information:

Download Benchmarks

@andrewbranch andrewbranch requested a review from jakebailey March 27, 2024 23:49
@@ -5626,6 +5626,7 @@ export interface EmitResolver {
isEntityNameVisible(entityName: EntityNameOrEntityNameExpression, enclosingDeclaration: Node): SymbolVisibilityResult;
// Returns the constant value this property access resolves to, or 'undefined' for a non-constant
getConstantValue(node: EnumMember | PropertyAccessExpression | ElementAccessExpression): string | number | undefined;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this still need to have EnumMember?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not, but the same signature is public API on TypeChecker so maybe it’s best not to change it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, declarations.ts still calls getConstantValue but only on an enum value, which makes me think that the resolver wouldn't need that func at all anymore. But, ts.ts's tryGetConstEnumValue still calls it for property access / element accesses :(

@typescript-bot
Copy link
Collaborator

@andrewbranch Here are the results of running the top 400 repos comparing main and refs/pull/57966/merge:

Everything looks good!

@@ -167,6 +167,8 @@ import {
equateValues,
escapeLeadingUnderscores,
escapeString,
type EvaluatorResult,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nit but we haven't really used type imports anywhere else quite yet (besides that protocol.ts blunder); I don't think we've set the settings in settings.json yet that would allow sorting to be inline either... Maybe we just avoid type imports until we can switch them all?

Copy link
Member Author

@andrewbranch andrewbranch Apr 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine with me, but this is what auto-imports did automatically 😬 My bad, that’s my own setting; I guess I was testing it a while back. It works!

Copy link
Member

@jakebailey jakebailey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems good to me, though I'm still hoping that we can reduce the EmitResolver API surface since there are so few calls to the old API left...

Maybe @weswigham @dragomirtitian have some feedback on that front just from needing to implement that resolver in a minimal fashion, but given this code all exists and works via the existing evaluator that was just pulled out, I doubt this is really a big deal.

@dragomirtitian
Copy link
Contributor

dragomirtitian commented Apr 3, 2024

Maybe @weswigham @dragomirtitian have some feedback on that front just from needing to implement that resolver in a minimal fashion.

This looks ok to me from the point of view of creating a minimalist resolver.

@andrewbranch andrewbranch merged commit 9f8a231 into microsoft:main Apr 4, 2024
25 checks passed
@andrewbranch andrewbranch deleted the bug/isolated-modules-enum-restrictions branch April 4, 2024 16:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants